﻿
@{
    if (ViewData["title"] == null)
    {
        ViewBag.Title = "创建新页面";
    }
    else
    {
        ViewBag.Title = ViewData["title"];
    }
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<h2>创建新页面</h2>
<div class="container">
    <div class="row">
        <div class="col-md-6">
            @{
                if (ViewData["title"] == null)
                {<input type="text" class="form-control" id="title" placeholder="输入标题" style="width:100%" />}
                else
                {
                    <input type="text" class="form-control" id="title" placeholder="输入标题" style="width:100%" value="@ViewData["title"]"/>
                }
            }
            
        </div>
    </div>
    <div class="row">
        <div class="col-md-6">
            <textarea class="form-control" rows="20" id="content" onkeyup="compile()"></textarea>
        </div>
        <div class="col-md-12" id="result">
        </div>
    </div>
    <div class="row">
        <div class="col-md-2">
            <button class="btn btn-primary" onclick="save()">保存</button>
        </div>
    </div>
    @*<div class="row">
        
    </div>*@

</div>
<div id="note">创建成功</div>

<script type="text/javascript">
    window.onload = function () {
        var oTxt = document.getElementById("content");
        oTxt.onkeydown = function (ev) {
            var oEvent = ev || event;
            if (oEvent.keyCode == 9) {

                /*关键部分 开始(重要! 去掉浏览器的默认事件 不然会按tab之后光标会跳去其他的地方,用户优化不好)*/
                if (oEvent.preventDefault) {
                    oEvent.preventDefault();
                }
                else {
                    window.event.returnValue = false;
                }
                /*关键部分 结束*/
                oTxt.value += "    "; //这里放入了4个空格
            }
        }
    }
    function compile() {
        var text = document.getElementById("content").value;
        var converter = new showdown.Converter();
        var html = converter.makeHtml(text);
        document.getElementById("result").innerHTML = html;
    }
    function save() {
        var text = document.getElementById("content").value;
        var t = document.getElementById("title").value;
        var postdata = {
            title: t,
            data: text
        };
        $.post("/Create/New", postdata, function (resp) {
           
            if (resp.result == "ok") {
                alert("保存成功");
                var url = "/" + resp.title;
                window.location.href = url;
               
            }
            else
            {
                alert(resp.result);              
            }
        },"json");

    }
    //function out() {
    //    $('#note').animate({ top: '0' }, 500, function () {
    //        $(this).css({ display: 'none', top: '-100px' });
    //    });
    //}

</script>

@section scripts{

    <script src="~/Scripts/showdown.min.js"></script>
    <script src="~/Scripts/jquery-2.2.0.min.js"></script>
}


